﻿Imports AugmentLib.Database
Imports System.Configuration.ConfigurationManager
Partial Class country
    Inherits System.Web.UI.Page

#Region " Properties "

    Public Property CurrentPage() As Integer
        Get
            'Looks for the current page in ViewState
            Dim objPage As Object = ViewState("_CurrentPage")
            If objPage Is Nothing Then
                Return 0
            Else
                'default to showing the first page
                Return CInt(objPage)
            End If
        End Get
        Set(ByVal value As Integer)
            ViewState("_CurrentPage") = value
        End Set
    End Property

    Public Property PageSize() As Integer
        Get
            'Looks for the current page in ViewState
            Dim objPageSize As Object = ViewState("_PageSize")
            If objPageSize Is Nothing Then
                Return 10
            Else
                'default to showing the first page
                Return CInt(objPageSize)
            End If
        End Get
        Set(ByVal value As Integer)
            ViewState("_PageSize") = value
        End Set
    End Property

#End Region

#Region " Paging and Page Size "

    Protected Sub lnkHead10Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead10Item.Click
        PageSize = 10
      LoadCountry()
    End Sub

    Protected Sub lnkHead25Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead25Item.Click
        PageSize = 25
      LoadCountry()
    End Sub

    Protected Sub lnkHead100Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead100Item.Click
        PageSize = 100
       LoadCountry()
    End Sub

    Protected Sub lnkHead150Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkHead150Item.Click
        PageSize = 150
        LoadCountry()
    End Sub

    Protected Sub lnkFoot10Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot10Item.Click
        PageSize = 10
       LoadCountry()
    End Sub

    Protected Sub lnkFoot25Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot25Item.Click
        PageSize = 25
       LoadCountry()
    End Sub

    Protected Sub lnkFoot100Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot100Item.Click
        PageSize = 100
       LoadCountry()
    End Sub

    Protected Sub lnkFoot150Item_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFoot150Item.Click
        PageSize = 150
       LoadCountry()
    End Sub

    Protected Sub btnMoveFirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMoveFirst.Click
        CurrentPage = 0
        txtPageNo.Text = CurrentPage + 1
        LoadCountry()
    End Sub

    Protected Sub btnPrevious_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
        If CurrentPage > 0 Then
            CurrentPage = CurrentPage - 1
            txtPageNo.Text = CurrentPage + 1
          LoadCountry()
        End If
    End Sub

    Protected Sub btnGo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGo.Click
        If CInt(txtPageNo.Text < 1) Then
            txtPageNo.Text = 1
        End If

        If CInt(txtPageNo.Text > CInt(lblMaxPageCount.Text)) Then
            txtPageNo.Text = CInt(lblMaxPageCount.Text) + 1
        End If

        CurrentPage = CInt(txtPageNo.Text) - 1
       LoadCountry()

    End Sub

    Protected Sub btnNext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNext.Click
        If CurrentPage < CInt(lblMaxPageCount.Text) Then
            CurrentPage = CurrentPage + 1
            txtPageNo.Text = CurrentPage + 1
           LoadCountry()
        End If
    End Sub

    Protected Sub btnMoveLast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnMoveLast.Click
        CurrentPage = CInt(lblMaxPageCount.Text)
        txtPageNo.Text = CurrentPage + 1
       LoadCountry()
    End Sub

#End Region
    
    'Added @ 1/4/2011 by minginn to allow user to sort the result
    Private Sub LoadCountry()

        Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
        Dim TempDT As New Data.DataTable

        Dim sortColumn As String = "Country"
        Try
            objDB.OpenConnection()
            If txtSearch.Text = "" Then
                objDB.dbVariable.SQLString = "SELECT CountryId,Country FROM AW_Country WHERE StatusDelete=0 ORDER BY " & sortColumn & " " & lblhidSortType.Text
                objDB.FillData(TempDT, "AW_Country", Database.SQLCommandType.NormalString)
            Else
                objDB.dbVariable.SQLString = "SELECT CountryId,Country FROM AW_Country WHERE StatusDelete=0 AND Country LIKE @Country ORDER BY " & sortColumn & " " & lblhidSortType.Text
                objDB.AddParameter("Country", Data.SqlDbType.NVarChar, "%" & txtSearch.Text & "%")
                objDB.FillData(TempDT, "AW_Country", Database.SQLCommandType.NormalString)
            End If
            objDB.CloseConnection()
        Catch ex As Exception

        Finally
            If (objDB IsNot Nothing) Then
                objDB.CloseConnection()
            End If
        End Try
        'Creating a Paged Datasouce
        Dim objPaged As New PagedDataSource
        objPaged.DataSource = TempDT.DefaultView
        objPaged.AllowPaging = True
        objPaged.PageSize = PageSize
        objPaged.CurrentPageIndex = CurrentPage

        lblHeadNoOfItem.Text = TempDT.Rows.Count
        lblFootNoOfItem.Text = TempDT.Rows.Count

        repCountry.DataSource = objPaged
        repCountry.DataBind()

        lblMaxPageCount.Text = objPaged.PageCount - 1
        lblTotalPage.Text = " of " & objPaged.PageCount
    End Sub

    Protected Sub imgNewCountry_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles imgNewCountry.Click
        ClearAllField()
        mpeEntry.Show()
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Session("StaffId") Is Nothing Then
            Response.Redirect("Default.aspx")
        ElseIf Session.Item("StaffType").ToString = "2" Then
            Response.Redirect("Default.aspx")
        Else
            lblStaffUID.Text = Session("StaffId").ToString
        End If

        If Not Page.IsPostBack Then
            LoadCountry()
        End If
    End Sub

    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        LoadCountry()
    End Sub

    Protected Sub repCountry_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles repCountry.ItemCommand

        If e.CommandName.ToUpper = "EDITITEM" Then
            txtCountry.Text = CType(repCountry.Items(e.CommandArgument).FindControl("lblRepCountry"), Label).Text
            lblCountryUID.Text = CType(repCountry.Items(e.CommandArgument).FindControl("lblRepCountryUID"), Label).Text
            mpeEntry.Show()
        Else
            'Begin Delete
            Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
            Try
                objDB.OpenConnection()
                objDB.dbVariable.SQLString = "UPDATE AW_Country SET StatusDelete=1 WHERE CountryId=@CountryId"
                objDB.AddParameter("CountryId", Data.SqlDbType.UniqueIdentifier, New Guid(CType(repCountry.Items(e.CommandArgument).FindControl("lblRepCountryUID"), Label).Text))
                objDB.ExecuteQuery()
                objDB.CloseConnection()
            Catch ex As Exception

            Finally
                If (objDB IsNot Nothing) Then
                    objDB.CloseConnection()
                End If
            End Try
            lblMsgSuccessful.Text = "Country Deleted"
            lblMsgSuccessful.Visible = True
            LoadCountry()
        End If
    End Sub

    Private Sub ClearAllField()
        txtCountry.Text = String.Empty
        lblCountryUID.Text = String.Empty
        divError.Visible = False
        divSuccessful.Visible = False
    End Sub

    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click

        If lblCountryUID.Text = "" Then
            'New
            Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
            Try
                objDB.OpenConnection()
                Dim ExistCountry As String
                ExistCountry = CountryExist(objDB)
                If ExistCountry = "0" Then
                    ClearAllField()
                    divError.Visible = True
                    lblMsgError.Text = "Country Already Exist"
                    LoadCountry()
                ElseIf ExistCountry <> "" Then
                    objDB.dbVariable.SQLString = "UPDATE AW_Country SET StatusDelete=@StatusDelete WHERE CountryId=@CountryId"
                    objDB.AddParameter("CountryId", Data.SqlDbType.UniqueIdentifier, New Guid(ExistCountry))
                    objDB.AddParameter("StatusDelete", Data.SqlDbType.TinyInt, 0)
                    objDB.ExecuteQuery()
                    objDB.CloseConnection()
                    ClearAllField()
                    txtSearch.Text = String.Empty
                    divSuccessful.Visible = True
                    lblMsgSuccessful.Text = "Country Successfully Updated"
                    LoadCountry()
                Else
                    objDB.dbVariable.SQLString = "INSERT INTO AW_Country (CountryId,Country,CreateDate,StatusDelete) VALUES (@CountryId,@Country,@CreateDate,@StatusDelete)"
                    objDB.AddParameter("CountryId", Data.SqlDbType.UniqueIdentifier, Guid.NewGuid)
                    objDB.AddParameter("Country", Data.SqlDbType.NVarChar, txtCountry.Text.Trim.ToUpper)
                    objDB.AddParameter("CreateDate", Data.SqlDbType.DateTime, Now)
                    objDB.AddParameter("StatusDelete", Data.SqlDbType.TinyInt, 0)
                    objDB.ExecuteQuery()
                    ClearAllField()
                    txtSearch.Text = String.Empty
                    divSuccessful.Visible = True
                    lblMsgSuccessful.Text = "Country Successfully Inserted"
                    LoadCountry()
                End If
                objDB.CloseConnection()
            Catch ex As Exception

            Finally
                If (objDB IsNot Nothing) Then
                    objDB.CloseConnection()
                End If
            End Try
        Else
            'Edit
            Dim objDB As New Database(AppSettings("datasource"), AppSettings("database"), AppSettings("username"), AppSettings("password"), AppSettings("timeout"), AppSettings("pooling"))
            Try
                objDB.OpenConnection()
                objDB.dbVariable.SQLString = "UPDATE AW_Country SET Country=@Country WHERE CountryId=@CountryId"
                objDB.AddParameter("CountryId", Data.SqlDbType.UniqueIdentifier, New Guid(lblCountryUID.Text))
                objDB.AddParameter("Country", Data.SqlDbType.NVarChar, txtCountry.Text.Trim.ToUpper)
                objDB.ExecuteQuery()
                objDB.CloseConnection()
                ClearAllField()
                divSuccessful.Visible = True
                lblMsgSuccessful.Text = "Country Successfully Updated"
                LoadCountry()
            Catch ex As Exception

            Finally
                If (objDB IsNot Nothing) Then
                    objDB.CloseConnection()
                End If
            End Try
        End If

    End Sub

    Private Function CountryExist(ByRef objDB As Database) As String
        Dim Tempdt As New Data.DataTable
        Dim strCountry As String = String.Empty

        objDB.dbVariable.SQLString = "SELECT Country, StatusDelete, CountryId FROM AW_Country WHERE Country=@Country"
        objDB.AddParameter("Country", Data.SqlDbType.NVarChar, txtCountry.Text.Trim.ToUpper)
        objDB.FillData(Tempdt, "AW_Country", Database.SQLCommandType.NormalString)
        objDB.ClearParameter()
        If Tempdt.Rows.Count > 0 Then
            For Each row As Data.DataRow In Tempdt.Rows
                If row.Item("StatusDelete").ToString = "0" Then
                    strCountry = "N"
                Else
                    strCountry = row.Item("CountryId").ToString
                End If
            Next
        Else
            strCountry = ""
        End If
        Return strCountry
    End Function

    Protected Sub lnkCountry_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkCountry.Click
        SortType()
        LoadCountry()
    End Sub

    Private Sub SortType()
        If lblhidSortType.Text = "ASC" Then
            lblhidSortType.Text = "DESC"
        Else
            lblhidSortType.Text = "ASC"
        End If
    End Sub

    Protected Sub repCountry_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles repCountry.ItemDataBound

        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then

            CType(e.Item.FindControl("cbeDelete"), Object).confirmText = "Are you sure you want to delete " & CType(e.Item.FindControl("lblRepCountry"), Label).Text
        End If

    End Sub
End Class
